package com.zmj.servie;


import com.zmj.exception.CustomizeException;
import com.zmj.exception.ErrorCodeEnum;
import com.zmj.mapper.demo.StudentMapper;
import com.zmj.pojo.Student;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


@Slf4j
@Service
public class HelloServiceImpl {

    @Resource
    private StudentMapper studentMapper;


    public void exception(String errorType) {
        log.error("errorType {}", errorType);
        //模拟业务异常
        if (errorType.equalsIgnoreCase("error")) {
            throw new CustomizeException(ErrorCodeEnum.TEST_ERROR);
        }
        //模拟系统异常处理
        if (errorType.equalsIgnoreCase("sys")) {
            int acc = 1 / 0;
        }
    }

    @Async
    public void sync() {
        System.out.println(Thread.currentThread().getName() + " HelloServiceImpl run ...");
    }

    @Cacheable(value = "student_cache", key = "#id", unless = "#result == null")
    public Student cache(Integer id) {
        return studentMapper.selectById(id);
    }

    @CacheEvict(value = "student_cache", key = "#id")
    public void delCache(Integer id) {
        studentMapper.deleteById(id);
    }
}

